package cn.itcast.mobilebrowser.adapter;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import cn.itcast.mobilebrowser.R;
import cn.itcast.mobilebrowser.model.WebTab;

/**
 * 继承了ListAdapter的适配器
 */
public class WebTabListAdapter extends ListAdapter<WebTab, WebTabListAdapter.MyViewHolder> {

    // 定义回调接口
    public interface OnTabClickListener {
        void onTabClick(View view, int position);
        void onTabCloseClick(View view, int position);
    }

    private final OnTabClickListener mOnTabClickListener;

    public WebTabListAdapter(OnTabClickListener mOnTabClickListener) {
        // 为了在调用父类之前得到这个callback，需要将其设为静态
        super(WebTabListAdapter.CALLBACK);
        this.mOnTabClickListener = mOnTabClickListener;
    }

    @NonNull
    @NotNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_tab, parent, false);
        return new WebTabListAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull MyViewHolder holder, int position) {
        // list adapter提供了 getItem 来获取数据对象
        WebTab webTab = this.getItem(position);
        holder.bind(webTab);
        // 设置点击监听器
        if (mOnTabClickListener != null) {
            // 设置关闭
            holder.closeButton.setOnClickListener( v -> {
                mOnTabClickListener.onTabCloseClick(holder.closeButton, position);
            });
            // 设置点击打开
            holder.webImage.setOnClickListener(v -> {
                mOnTabClickListener.onTabClick(holder.webImage, position);
            });
        }
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView webTitle;
        public ImageView webImage;
        // 删除键
        public ImageView closeButton;

        public MyViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            webTitle = itemView.findViewById(R.id.web_title);
            webImage = itemView.findViewById(R.id.web_tab_image);
            closeButton = itemView.findViewById(R.id.close_tab_button);
        }

        /**
         * 将数据绑定视图
         * @param webTab 需要绑定的一项数据
         */
        public void bind(WebTab webTab) {
            //数据里的WebView不需要显示
            webTitle.setText(webTab.getTitle());
            Log.d("Adapter", "bind: " + webTab.getImage());
            // 裁切大小
            if (webTab.getImage() != null) {
                int imageWidth = webTab.getImage().getWidth();
                double scale = 1.0 * webImage.getLayoutParams().height /
                        webImage.getLayoutParams().width;
                // 裁切bitmap匹配大小
                Bitmap bitmap = Bitmap.createBitmap(webTab.getImage(), 0, 0,
                        imageWidth, (int) (imageWidth * scale));
                // 写回数据类
                webTab.setImage(bitmap);
                // 显示
                webImage.setImageBitmap(bitmap);
            } else {
                webImage.setImageBitmap(webTab.getImage());
            }
        }
    }

    public static Bitmap sizeBitmap(Bitmap origin, int newWidth, int newHeight) {
        if (origin == null) {
            return null;
        }
        int height = origin.getHeight();
        int width = origin.getWidth();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);// 使用后乘
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        if (!origin.isRecycled()) {//这时候origin还有吗？
            origin.recycle();
        }
        return newBM;
    }

    // ListAdapter所需的回调
    public static final DiffUtil.ItemCallback<WebTab> CALLBACK = new DiffUtil.ItemCallback<WebTab>() {

        @Override
        public boolean areItemsTheSame(@NonNull @NotNull WebTab oldItem,
                                       @NonNull @NotNull WebTab newItem) {
            return oldItem.getId() == newItem.getId();
        }

        @Override
        public boolean areContentsTheSame(@NonNull @NotNull WebTab oldItem,
                                          @NonNull @NotNull WebTab newItem) {
            // 允许重复内容添加
            return false;
        }
    };
}

